package com.wj.blog.controller;

import com.wj.blog.pojo.Blog;
import com.wj.blog.service.BlogService;
import com.wj.blog.utils.Page;
import com.wj.blog.utils.Result;
import com.wj.blog.vo.BlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author:   wj
 * Date:     2020/4/14 19:14
 * Description: 博客接口
 */
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    BlogService blogService;

    /**
     * 保存
     * @param blog
     * @return
     */
    @PostMapping("/save")
    public Result<Object> save(@RequestBody Blog blog){
        blogService.save(blog);
        return new Result<>("添加成功");
    }

    /**
     * 根据 id 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable String id){
        blogService.delete(id);
        return new Result<Object>("删除成功");
    }

    /**
     * 通过id查询阅读加一
     * @param id
     * @return
     */
    @GetMapping("/readId/{id}")
    public Result<Object> readId(@PathVariable String id){
       return new Result<Object>(blogService.readId(id));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<Object> getById(@PathVariable String id){
        return new Result<Object>(blogService.getById(id));
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @PostMapping("/getByPage")
    public Result<Page<BlogVo>> getByPage(@RequestBody Page<BlogVo> page){
        return new Result<>(blogService.getByPage(page));
    }
}
